package com.sky.service.impl;

import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.WorkSpaceMapper;
import com.sky.service.OrderService;
import com.sky.service.UserService;
import com.sky.service.WorkSpaceService;
import com.sky.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    WorkSpaceMapper workSpaceMapper;
    @Autowired
    OrderService orderService;
    @Autowired
    UserService userService;
    @Autowired
    DishMapper dishMapper;

    /**
     * 工作台今日数据查询
     * */
    @Override
    public BusinessDataVO businessData() {
        //获取今天的时间
        LocalDate now = LocalDate.now();
        LocalDateTime start = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(now, LocalTime.MAX);
        //查询营业额，状态是已完成的订单的总金额
       Double turnover = orderService.getTurnover(start,end);
        //查询有效订单
        int validOrderCount = orderService.countValidOrders(start,end);
        //查询总订单数
        int totalOrders = orderService.countTotalOrders(start,end);
        //计算完成率
        Double orderCompletionRate = (totalOrders<=0? 0:validOrderCount * 1.0/totalOrders);
        //计算平均客单价
        Double unitPrice = (validOrderCount<0? 0:turnover/validOrderCount);
        //查询新增用户
        int newUser = userService.contNewUser(start, end);
        //封装返回
        BusinessDataVO vo = new BusinessDataVO();
        vo.setTurnover(turnover);
        vo.setNewUsers(newUser);
        vo.setUnitPrice(unitPrice);
        vo.setValidOrderCount(validOrderCount);
        vo.setOrderCompletionRate(orderCompletionRate);
        return vo;
    }
    /**
     * 查询订单管理
     * */
    @Override
    public OrderOverViewVO overviewOrders() {
        // 获取今天的时间
        LocalDate now = LocalDate.now();
        LocalDateTime start = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(now, LocalTime.MAX);
        List<OrderStatusCountVO> voList = orderService.groupCountByStatusAndCreateTime(start, end);
        if(CollectionUtils.isEmpty(voList)){
            return new OrderOverViewVO();
        }
        // 封装返回
        OrderOverViewVO vo = new OrderOverViewVO();
        // 所有
        vo.setAllOrders(voList.stream().map(OrderStatusCountVO::getCount).reduce(0, (a,b)->a+b));
        // 待接单
        vo.setWaitingOrders(voList.stream().filter(e-> Objects.equals(e.getStatus(), Orders.TO_BE_CONFIRMED)).map(OrderStatusCountVO::getCount).reduce(0, (a,b)->a+b));
        //待派送
        vo.setDeliveredOrders(voList.stream().filter(e-> Objects.equals(e.getStatus(), Orders.CONFIRMED)).map(OrderStatusCountVO::getCount).reduce(0, (a,b)->a+b));
        // 已完成
        vo.setCompletedOrders(voList.stream().filter(e-> Objects.equals(e.getStatus(), Orders.COMPLETED)).map(OrderStatusCountVO::getCount).reduce(0, (a,b)->a+b));
        // 已取消
        vo.setCancelledOrders(voList.stream().filter(e-> Objects.equals(e.getStatus(), Orders.CANCELLED)).map(OrderStatusCountVO::getCount).reduce(0, (a,b)->a+b));
        return vo;
    }

//    private int getOrdersCountByStatus(List<OrderStatusCountVO> voList, int status){
//        return voList.stream()
//                .filter(e-> Objects.equals(e.getStatus(), status))
//                .map(OrderStatusCountVO::getCount)
//                .reduce(0, (a,b)->a+b);
//    }

    @Override
    public DishOverViewVO overviewDishes() {
        Map map = new HashMap();
        //查询起售中的菜品
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);
        //查询未起售的菜品
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);
        //封装返回
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
    /**
     *查询套餐总览
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Map map = new HashMap();
        //查询起售中的套餐
        map.put("status",StatusConstant.ENABLE);
        Integer sold = dishMapper.countByDishMap(map);
        //查询未起售的套餐
        map.put("status",StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByDishMap(map);
        //封装返回
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}

